package com.xgq.drink.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

/**
 * 登录检查切面
 * 用于拦截使用@RequiresLogin注解的方法，检查用户是否已登录
 */
@Aspect
@Component
public class LoginCheckAspect {
    
    @Around("@annotation(com.xgq.drink.annotation.RequiresLogin)")
    public Object checkLogin(ProceedingJoinPoint joinPoint) throws Throwable {
        // 获取当前用户认证信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        
        // 检查用户是否已登录
        if (authentication == null || !authentication.isAuthenticated()) {
            throw new RuntimeException("用户未登录");
        }
        
        // 执行原方法
        return joinPoint.proceed();
    }
} 